package cn.dm.util;

import redis.clients.jedis.Jedis;

import java.util.function.Function;

/**
 * 自己随便封装的,仅为了简写代码
 * 若是将来使用时网上找个比较完善的
 */
public class JedisUtils {
    public static Jedis getJedis(String host, int port) {
        Jedis jedis = new Jedis(host, port);
        return jedis;
    }


    public static Jedis getJedis(String host, int port, String password) {
        Jedis jedis = new Jedis(host, port);
        jedis.auth(password);
        return jedis;
    }

    /**
     * 每次执行正轨代码均要写在try中,因此把catch和finally固定写死
     * 将来传入函数式接口实现不同逻辑
     */
    public static void excute(Jedis jedis, Function<Jedis, Void> function) {
        try {
            function.apply(jedis);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
